﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;
using UnityEditor.IMGUI.Controls;

namespace HDJ.Framework.Core
{
    public class LanguageEditorWindow : EditorWindow
    {


        private SystemLanguage currentLanguage;
        private TableData currentFileDataTable;
        private LanguageSettingData config;

        //所有文件（转换成全路径/）
        static List<string> s_languageFullKeyFileNameList = new List<string>();

        static LanguageEditorWindow win = null;

        [MenuItem("Tool/多语言管理器", priority = 1103)]
        public static LanguageEditorWindow ShowWindow()
        {
            win = EditorWindow.GetWindow<LanguageEditorWindow>();
            win.Init();
            EditorFramework.OnRecreatePathFile += OnRecreatePathFile;
            return win;
        }

        private static void OnRecreatePathFile()
        {
            TableManager.Clear();
        }

        FolderTreeView treeView;
        TreeViewState treeViewState = null;

        private void Init()
        {
            win = this;
            if (ResourceManager.ExistAssets(LanguageManager.LanguagesSettingFileName))
            {
                config = LanguageManager.LoadLocalizedLanguageSettingData();
                if (!config.gameExistLanguages.Contains(currentLanguage))
                {
                    currentLanguage = config.defaultLanguage;
                }
                s_languageFullKeyFileNameList = LanguageUtils.LoadLangusgeAllFileNames(currentLanguage);
            }

            if (!string.IsNullOrEmpty(selectFullFileName))
            {
                currentFileDataTable = LoadFileData(currentLanguage, selectFullFileName);
            }
  
                treeViewState = new TreeViewState();

            treeView = new FolderTreeView(treeViewState);

            treeView.SetData(s_languageFullKeyFileNameList);
            treeView.dblclickItemCallBack = ModuleFileDblclickItemCallBack;
            treeView.selectCallBack = ModuleFileFolderSelectCallBack;
        }
        void OnEnable()
        {
            Init();
        }
        private void OnDestroy()
        {
            treeViewState = null;
        }
        public int toolbarOption = 0;
        private string[] toolbarTexts = { "模块文件", "语言内容编辑", "语言设置" };
        private bool richText = false;
        void OnGUI()
        {
            titleContent.text = "多语言编辑器";
            if (config == null)
            {
                AddLanguageGUI();
                return;
            }
            richText = (bool)EditorDrawGUIUtil.DrawBaseValue("使用富文本：", richText);
            SelectLanguageGUI();
            DefaultLanguageGUI();
            SelectEditorModuleGUI();
            SearchValueGUI();
            toolbarOption = GUILayout.Toolbar(toolbarOption, toolbarTexts, GUILayout.Width(Screen.width));
            switch (toolbarOption)
            {
                case 0:
                    EditorLanguageModuleFileGUI();
                    break;
                case 1:
                    EditorLanguageFieldGUI();
                    break;
                case 2:
                    ConfigSettingGUI();
                    AddLanguageGUI();
                    DeleteLanguageGUI();
                    break;
            }

            GUILayout.FlexibleSpace();
            SaveDataGUI();
        }
        public string searchValue;
        private string searchKey = "";
        private void SearchValueGUI()
        {
            searchValue = EditorDrawGUIUtil.DrawSearchField(searchValue);

            if (!string.IsNullOrEmpty(searchValue))
            {
                if (searchValue.Contains("/"))
                {
                    string[] tempV = searchValue.Split('/');
                    string key = tempV[tempV.Length - 1];
                    int indexEnd = searchValue.LastIndexOf("/");
                    string moduleName = searchValue.Remove(indexEnd);
                    if (s_languageFullKeyFileNameList.Contains(moduleName))
                    {
                        if (selectFullFileName != moduleName)
                        {
                            selectFullFileName = moduleName;
                            //Debug.Log("moduleName :" + moduleName);
                            currentFileDataTable = LoadFileData(currentLanguage, selectFullFileName);
                            //Debug.Log("currentFileDataTable :" + currentFileDataTable);
                            //Debug.Log("  keys:" + currentFileDataTable.TableIDs.Count);
                        }
                        searchKey = key;
                    }
                    else
                    {
                        selectFullFileName = "";
                        searchKey = "";
                    }

                }
                else
                    searchKey = searchValue;
            }
            else
            {
                searchKey = "";
            }
        }

        #region 语言设置

        void DefaultLanguageGUI()
        {
            if (currentLanguage == config.defaultLanguage)
            {
                EditorGUI.indentLevel = 1;
                EditorGUILayout.LabelField("默认语言");
            }
        }

        void ConfigSettingGUI()
        {
            config.defaultLanguage = EditorDrawGUIUtil.DrawPopup("默认语言", config.defaultLanguage, config.gameExistLanguages);
            string lanNames = "";
            foreach (var item in config.gameExistLanguages)
            {
                lanNames += " " + item;
            }
            EditorGUILayout.LabelField("已含有语言：" + lanNames);
        }

        #endregion

        #region 选择语言

        void SelectLanguageGUI()
        {
            GUILayout.BeginHorizontal();
            currentLanguage = EditorDrawGUIUtil.DrawPopup("当前语言", currentLanguage, config.gameExistLanguages, (lan) =>
            {
                if (!string.IsNullOrEmpty(selectFullFileName))
                {
                    currentFileDataTable = LoadFileData(lan, selectFullFileName);
                }
            });
            if (currentLanguage == SystemLanguage.Chinese)
                currentLanguage = SystemLanguage.ChineseSimplified;

            if (GUILayout.Button("加载上一次保存"))
            {

                TableManager.Clear();
                currentFileDataTable = LoadFileData(currentLanguage, selectFullFileName);
                GUI.FocusControl("");
            }
            GUILayout.EndHorizontal();
        }
        #endregion

        #region 编辑语言字段

        private string selectFullFileName = "";
        private void SelectEditorModuleGUI()
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label("选择编辑模块");
            GUILayout.FlexibleSpace();
            EditorGUILayout.SelectableLabel(selectFullFileName);
            GUILayout.EndHorizontal();
        }
        /// <summary>
        /// 模块文件中双击操作，选择文件
        /// </summary>
        /// <param name="t"></param>
        private void ModuleFileDblclickItemCallBack(FolderTreeViewItem t)
        {
            if (t.isDirectory)
                return;

            selectFullFileName = t.fullPath;
            currentFileDataTable = LoadFileData(currentLanguage, selectFullFileName);
            toolbarOption = 1;
        }
        /// <summary>
        /// 模块文件中单击选择文件
        /// </summary>
        /// <param name="t"></param>
        private void ModuleFileFolderSelectCallBack(FolderTreeViewItem t)
        {
            //Debug.Log(t.fullPath+ " depth :" + t.depth +" isDir :"+t.isDirectory);
            if (t.isDirectory)
                return;

            selectItemFullName = t.fullPath;
        }
        private string selectItemFullName = "";
        void EditorLanguageModuleFileGUI()
        {
            GUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("▲多语言模块列表(双击选择文件)");
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("新增模块", GUILayout.Width(70)))
            {
                AddLanguageModelGUI();
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(9);
            GUIStyle style = "box";
            if (!string.IsNullOrEmpty(selectItemFullName))
            {
                style = "U2D.createRect";
            }
            GUILayout.BeginHorizontal(style);
            GUILayout.Label("选择的文件：" + selectItemFullName);
            if (!string.IsNullOrEmpty(selectItemFullName))
            {
                if (GUILayout.Button("删除", GUILayout.Width(40)))
                {

                    if (EditorUtility.DisplayDialog("提示", "确定删除 :" + selectItemFullName, "OK", "Cancel"))
                    {
                        if (selectItemFullName == selectFullFileName)
                            selectFullFileName = "";
                        s_languageFullKeyFileNameList.Remove(selectItemFullName);


                        foreach (var lan in config.gameExistLanguages)
                        {
                            string path = GetLanguageSavePath(lan, selectItemFullName);
                            FileUtils.DeleteFile(path);
                        }
                        // SaveData();
                        AssetDatabase.Refresh();
                        selectItemFullName = "";
                        OnEnable();
                    }

                }
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(8);
            Rect rect = GUILayoutUtility.GetRect(0, 100000, 0, 100000);
            treeView.OnGUI(rect);

        }

        void AddLanguageModelGUI()
        {
            GeneralDataModificationWindow.OpenWindow(this, "新增模块", "", (value) =>
            {
                value = EditorDrawGUIUtil.DrawBaseValue("模块名", value);
                string tempName = value.ToString();
                if (string.IsNullOrEmpty(tempName))
                    EditorGUILayout.HelpBox("名字不能为空", MessageType.Error);
                if (s_languageFullKeyFileNameList.Contains(tempName.Replace('@', '/')))
                    EditorGUILayout.HelpBox("名字重复", MessageType.Error);

                EditorGUILayout.HelpBox("文件夹使用@分割如：TTT@PPP", MessageType.Info);
                return value;
            }, (value) =>
            {
                string tempName = value.ToString();
                if (string.IsNullOrEmpty(tempName))
                    return false;
                if (s_languageFullKeyFileNameList.Contains(tempName.Replace('@', '/')))
                    return false;
                return true;
            }, (value) =>
            {
                string fileName = value.ToString();
                CreateNewFile(fileName, null);
                string tempContent = fileName.Replace('@', '/');
                selectFullFileName = tempContent;
                //SaveData();
                Init();
            });

        }


        void EditorLanguageFieldGUI()
        {
            if (string.IsNullOrEmpty(selectFullFileName))
                return;
            if (currentFileDataTable == null)
                return;
            TableData data = currentFileDataTable;
            List<string> languageKeyList = new List<string>(data.datas.Keys);
            //Debug.Log("languageKeyList :" + languageKeyList.Count);
            AddLangeuageFieldGUI(languageKeyList);

            EditorGUILayout.Space();

            EditorDrawGUIUtil.DrawScrollView(languageKeyList, () =>
            {
                for (int i = 0; i < languageKeyList.Count; i++)
                {
                    string key = languageKeyList[i];
                    if (!string.IsNullOrEmpty(searchKey))
                        if (!key.Contains(searchKey))
                            continue;

                    GUILayout.Space(5);
                    GUILayout.BeginVertical("HelpBox");
                    EditorGUILayout.BeginHorizontal();

                    string content = "";
                    if (data != null)
                    {
                        //if (!data.datas.ContainsKey(key))
                        //{
                        //    SingleData sd = new SingleData();
                        //    sd.Add(LanguageManager.c_mainKey, key);
                        //    sd.Add(LanguageManager.c_valueKey, "");
                        //    data.AddData(sd);
                        //}
                        content = data.datas[key][LanguageManager.LanTableValue].ToString();
                    }

                    if (GUILayout.Button("X", GUILayout.Width(20)))
                    {
                        if (EditorUtility.DisplayDialog("提示", "确定删除key", "OK", "Cancel"))
                        {
                            DeleteKey(selectFullFileName, key);
                            Init();
                            return;
                        }

                    }

                    GUILayout.Label(key);
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button("CopyPath"))
                    {
                        string tempContent = selectFullFileName;
                        tempContent += "/" + key;
                        TextEditor tx = new TextEditor();
                        tx.text = tempContent;
                        tx.OnFocus();
                        tx.Copy();
                        ShowNotification(new GUIContent("已复制"));
                    }

                    EditorGUILayout.EndHorizontal();
                    GUIStyle style = "TextArea";
                    style.wordWrap = true;
                    style.richText = richText;
                    content = EditorGUILayout.TextArea(content, style);
                    if (data != null)
                    {
                        data.datas[key][LanguageManager.LanTableValue] = content;
                    }
                    GUILayout.EndVertical();

                }
            }, "box");




        }

        string newField = "";

        void AddLangeuageFieldGUI(List<string> languageKeyList)
        {
            EditorGUILayout.LabelField("新增字段");
            if (true)
            {
                EditorGUI.indentLevel = 3;
                newField = EditorGUILayout.TextField("字段名", newField);

                if (newField != "" && !languageKeyList.Contains(newField))
                {
                    if (GUILayout.Button("新增语言字段"))
                    {
                        AddNewKey(selectFullFileName, newField);
                        Init();
                        newField = "";
                    }
                    EditorGUILayout.Space();
                }
                else
                {
                    if (languageKeyList.Contains(newField))
                    {
                        EditorGUILayout.LabelField("字段名重复！");
                    }
                }
            }
        }

        #endregion

        #region 编辑语言

        void DeleteLanguageGUI()
        {
            if (GUILayout.Button("删除语言"))
            {
                if (EditorUtility.DisplayDialog("警告", "确定要删除该语言吗！", "是", "取消"))
                {
                    config.gameExistLanguages.Remove(currentLanguage);
                    if (config.defaultLanguage == currentLanguage)
                    {
                        if (config.gameExistLanguages.Count > 0)
                            config.defaultLanguage = config.gameExistLanguages[0];
                        else
                            config.defaultLanguage = SystemLanguage.Unknown;

                    }
                    Directory.Delete(LanguageUtils.SaveFilePathDir + currentLanguage, true);
                    SaveData();
                    AssetDatabase.Refresh();
                }
            }
        }

        //void AddMissLanguageGUI(TableData data, string key)
        //{
        //    if (GUILayout.Button("添加记录"))
        //    {
        //        SingleData newData = new SingleData();
        //        newData.Add(LanguageManager.c_valueKey, "");
        //        newData.Add(LanguageManager.c_mainKey, key);

        //        data.AddData(newData);
        //    }
        //}

        void SaveDataGUI()
        {
            if (GUILayout.Button("保存"))
            {
                SaveData();
                ShowNotification(new GUIContent("已保存"));
            }
        }

        void SaveData()
        {
            string json = JsonUtils.ToJson(config);
            FileUtils.CreateTextFile(LanguageUtils.SaveFilePathDir + LanguageManager.LanguagesSettingFileName + ".txt", json);
            AssetDatabase.Refresh();

            SaveData(currentLanguage, selectFullFileName, currentFileDataTable);

            LanguageManager.Clear();

        }

        private void SaveData(SystemLanguage currentLanguage, string selectFullFileName, TableData currentFileDataTable)
        {
            if (currentFileDataTable == null || string.IsNullOrEmpty(selectFullFileName))
                return;
            string tText = TableUtils.TableData2Text(currentFileDataTable);
            string path = LanguageUtils.SaveFilePathDir + currentLanguage + "/" + GetLanguageSavePath(currentLanguage, selectFullFileName) + ".txt";
            FileUtils.CreateTextFile(path, tText);
            AssetDatabase.Refresh();

            if (EditorFramework.OnLanguageDataChange != null)
            {
                EditorFramework.OnLanguageDataChange();
            }
        }

        #endregion

        #region 新增语言

        void AddLanguageGUI()
        {
            if (GUILayout.Button("新增语言"))
            {
                GeneralDataModificationWindow.OpenWindow(this, "新增语言", SystemLanguage.Afrikaans, (value) =>
                {
                    SystemLanguage lan = (SystemLanguage)value;
                    lan = (SystemLanguage)EditorDrawGUIUtil.DrawBaseValue("语言：", lan);

                    if (config != null && config.gameExistLanguages.Contains(lan))
                    {
                        EditorGUILayout.HelpBox("已存在", MessageType.Error);
                    }
                    if (lan == SystemLanguage.Chinese)
                        lan = SystemLanguage.ChineseSimplified;
                    if (lan == SystemLanguage.Unknown)
                        lan = SystemLanguage.ChineseSimplified;
                    return lan;
                }, (value) =>
                {
                    SystemLanguage lan = (SystemLanguage)value;
                    if (config != null && config.gameExistLanguages.Contains(lan))
                    {
                        return false;
                    }
                    return true;
                }, (value) =>
                {
                    SystemLanguage lan = (SystemLanguage)value;

                    CreateNewLangusge(lan);

                }
                     );
            }
        }

        private void CreateNewLangusge(SystemLanguage lan)
        {
            if (config == null)
            {
                config = new  LanguageSettingData();
            }
            if (config.defaultLanguage == SystemLanguage.Unknown)
            {
                config.defaultLanguage = lan;
                currentLanguage = lan;
            }
            else
            {
                foreach (var item in s_languageFullKeyFileNameList)
                {
                    TableData dt = LoadFileData(config.defaultLanguage, item);
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    foreach (var id in dt.datas.Keys)
                    {
                        dic.Add(id, "");
                    }
                    CreateLanguageNewFile(lan, item, dic);
                }
            }
            config.gameExistLanguages.Add(lan);
            string path = LanguageUtils.SaveFilePathDir + lan;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            SaveData();
        }


        /// <summary>
        /// 新建多语言文件,给每种语言添加新文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="contentDic"></param>
        /// <returns>返回每个key对应的多语言访问key</returns>
        public void CreateNewFile(string fileName, Dictionary<string, string> contentDic)
        {
            string tempContent = fileName.Replace('@', '/');
            //给每种语言添加文件
            foreach (var item in config.gameExistLanguages)
            {
                CreateLanguageNewFile(item, tempContent, contentDic);
            }

        }

        public void CreateLanguageNewFile(SystemLanguage language, string fullKeyFileName, Dictionary<string, string> contentDic)
        {

            TableData data = new TableData();
            data.AddField(new TableFieldInfo(LanguageManager.LanTableKey, typeof(string)), data.fieldInfos.Count);
            data.AddField(new TableFieldInfo(LanguageManager.LanTableValue, typeof(string)), data.fieldInfos.Count);
            if (contentDic != null)
            {
                foreach (var item in contentDic)
                {
                    data.AddLineData(data.datas.Count, item.Key);
                    //keyPaths.Add(item.Key, fullKeyFileName + "/" + item.Key);
                }

            }
            SaveData(language, fullKeyFileName, data);
        }
        private void AddNewKey(string fullKeyFileName, string key)
        {
            foreach (var language in config.gameExistLanguages)
            {
                TableData data = LoadFileData(language, fullKeyFileName);
                Dictionary<string, object> values = new Dictionary<string, object>();
                values.Add(LanguageManager.LanTableKey, key);
                values.Add(LanguageManager.LanTableValue, "");
                data.AddLineData(data.datas.Count, key, values);

                SaveData(language, fullKeyFileName, data);
            }
        }

        private void DeleteKey(string fullKeyFileName, string key)
        {
            foreach (var language in config.gameExistLanguages)
            {
                TableData data = LoadFileData(language, fullKeyFileName);
                data.datas.Remove(key);
                SaveData(language, fullKeyFileName, data);
            }
        }

        #endregion

        public static TableData LoadFileData(SystemLanguage languageName, string fullFileKey)
        {
            string fileName = GetLanguageSavePath( languageName ,fullFileKey);

            TableData tableData = TableManager.GetTableData(fileName);

            return tableData;
        }

        public static string GetLanguageSavePath(SystemLanguage languageName, string fullFileKey)
        {
            return languageName + "_" + fullFileKey.Replace('/', '@');
        }
    }
}